home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / go-pear-bundle / PEAR.php < prev    next >
PHP Script  |  2004-03-25  |  30KB  |  971 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PEAR, the PHP Extension and Application Repository                   |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Sterling Hughes <sterling@php.net>                          |
  17. // |          Stig Bakken <ssb@php.net>                                   |
  18. // |          Tomas V.V.Cox <cox@idecnet.com>                             |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: PEAR.php,v 1.77 2004/02/07 04:42:07 cellog Exp $
  22. //
  23.  
  24. define('PEAR_ERROR_RETURN',     1);
  25. define('PEAR_ERROR_PRINT',      2);
  26. define('PEAR_ERROR_TRIGGER',    4);
  27. define('PEAR_ERROR_DIE',        8);
  28. define('PEAR_ERROR_CALLBACK',  16);
  29. define('PEAR_ERROR_EXCEPTION', 32);
  30. define('PEAR_ZE2', (function_exists('version_compare') &&
  31.                     version_compare(zend_version(), "2-dev", "ge")));
  32.  
  33. if (substr(PHP_OS, 0, 3) == 'WIN') {
  34.     define('OS_WINDOWS', true);
  35.     define('OS_UNIX',    false);
  36.     define('PEAR_OS',    'Windows');
  37. } else {
  38.     define('OS_WINDOWS', false);
  39.     define('OS_UNIX',    true);
  40.     define('PEAR_OS',    'Unix'); // blatant assumption
  41. }
  42.  
  43. // instant backwards compatibility
  44. if (!defined('PATH_SEPARATOR')) {
  45.     if (OS_WINDOWS) {
  46.         define('PATH_SEPARATOR', ';');
  47.     } else {
  48.         define('PATH_SEPARATOR', ':');
  49.     }
  50. }
  51.  
  52. $GLOBALS['_PEAR_default_error_mode']     = PEAR_ERROR_RETURN;
  53. $GLOBALS['_PEAR_default_error_options']  = E_USER_NOTICE;
  54. $GLOBALS['_PEAR_destructor_object_list'] = array();
  55. $GLOBALS['_PEAR_shutdown_funcs']         = array();
  56. $GLOBALS['_PEAR_error_handler_stack']    = array();
  57.  
  58. ini_set('track_errors', true);
  59.  
  60. /**
  61.  * Base class for other PEAR classes.  Provides rudimentary
  62.  * emulation of destructors.
  63.  *
  64.  * If you want a destructor in your class, inherit PEAR and make a
  65.  * destructor method called _yourclassname (same name as the
  66.  * constructor, but with a "_" prefix).  Also, in your constructor you
  67.  * have to call the PEAR constructor: $this->PEAR();.
  68.  * The destructor method will be called without parameters.  Note that
  69.  * at in some SAPI implementations (such as Apache), any output during
  70.  * the request shutdown (in which destructors are called) seems to be
  71.  * discarded.  If you need to get any debug information from your
  72.  * destructor, use error_log(), syslog() or something similar.
  73.  *
  74.  * IMPORTANT! To use the emulated destructors you need to create the
  75.  * objects by reference: $obj =& new PEAR_child;
  76.  *
  77.  * @since PHP 4.0.2
  78.  * @author Stig Bakken <ssb@php.net>
  79.  * @see http://pear.php.net/manual/
  80.  */
  81. class PEAR
  82. {
  83.     // {{{ properties
  84.  
  85.     /**
  86.      * Whether to enable internal debug messages.
  87.      *
  88.      * @var     bool
  89.      * @access  private
  90.      */
  91.     var $_debug = false;
  92.  
  93.     /**
  94.      * Default error mode for this object.
  95.      *
  96.      * @var     int
  97.      * @access  private
  98.      */
  99.     var $_default_error_mode = null;
  100.  
  101.     /**
  102.      * Default error options used for this object when error mode
  103.      * is PEAR_ERROR_TRIGGER.
  104.      *
  105.      * @var     int
  106.      * @access  private
  107.      */
  108.     var $_default_error_options = null;
  109.  
  110.     /**
  111.      * Default error handler (callback) for this object, if error mode is
  112.      * PEAR_ERROR_CALLBACK.
  113.      *
  114.      * @var     string
  115.      * @access  private
  116.      */
  117.     var $_default_error_handler = '';
  118.  
  119.     /**
  120.      * Which class to use for error objects.
  121.      *
  122.      * @var     string
  123.      * @access  private
  124.      */
  125.     var $_error_class = 'PEAR_Error';
  126.  
  127.     /**
  128.      * An array of expected errors.
  129.      *
  130.      * @var     array
  131.      * @access  private
  132.      */
  133.     var $_expected_errors = array();
  134.  
  135.     // }}}
  136.  
  137.     // {{{ constructor
  138.  
  139.     /**
  140.      * Constructor.  Registers this object in
  141.      * $_PEAR_destructor_object_list for destructor emulation if a
  142.      * destructor object exists.
  143.      *
  144.      * @param string $error_class  (optional) which class to use for
  145.      *        error objects, defaults to PEAR_Error.
  146.      * @access public
  147.      * @return void
  148.      */
  149.     function PEAR($error_class = null)
  150.     {
  151.         $classname = get_class($this);
  152.         if ($this->_debug) {
  153.             print "PEAR constructor called, class=$classname\n";
  154.         }
  155.         if ($error_class !== null) {
  156.             $this->_error_class = $error_class;
  157.         }
  158.         while ($classname) {
  159.             $destructor = "_$classname";
  160.             if (method_exists($this, $destructor)) {
  161.                 global $_PEAR_destructor_object_list;
  162.                 $_PEAR_destructor_object_list[] = &$this;
  163.                 break;
  164.             } else {
  165.                 $classname = get_parent_class($classname);
  166.             }
  167.         }
  168.     }
  169.  
  170.     // }}}
  171.     // {{{ destructor
  172.  
  173.     /**
  174.      * Destructor (the emulated type of...).  Does nothing right now,
  175.      * but is included for forward compatibility, so subclass
  176.      * destructors should always call it.
  177.      *
  178.      * See the note in the class desciption about output from
  179.      * destructors.
  180.      *
  181.      * @access public
  182.      * @return void
  183.      */
  184.     function _PEAR() {
  185.         if ($this->_debug) {
  186.             printf("PEAR destructor called, class=%s\n", get_class($this));
  187.         }
  188.     }
  189.  
  190.     // }}}
  191.     // {{{ getStaticProperty()
  192.  
  193.     /**
  194.     * If you have a class that's mostly/entirely static, and you need static
  195.     * properties, you can use this method to simulate them. Eg. in your method(s)
  196.     * do this: $myVar = &PEAR::getStaticProperty('myVar');
  197.     * You MUST use a reference, or they will not persist!
  198.     *
  199.     * @access public
  200.     * @param  string $class  The calling classname, to prevent clashes
  201.     * @param  string $var    The variable to retrieve.
  202.     * @return mixed   A reference to the variable. If not set it will be
  203.     *                 auto initialised to NULL.
  204.     */
  205.     function &getStaticProperty($class, $var)
  206.     {
  207.         static $properties;
  208.         return $properties[$class][$var];
  209.     }
  210.  
  211.     // }}}
  212.     // {{{ registerShutdownFunc()
  213.  
  214.     /**
  215.     * Use this function to register a shutdown method for static
  216.     * classes.
  217.     *
  218.     * @access public
  219.     * @param  mixed $func  The function name (or array of class/method) to call
  220.     * @param  mixed $args  The arguments to pass to the function
  221.     * @return void
  222.     */
  223.     function registerShutdownFunc($func, $args = array())
  224.     {
  225.         $GLOBALS['_PEAR_shutdown_funcs'][] = array($func, $args);
  226.     }
  227.  
  228.     // }}}
  229.     // {{{ isError()
  230.  
  231.     /**
  232.      * Tell whether a value is a PEAR error.
  233.      *
  234.      * @param   mixed $data   the value to test
  235.      * @param   int   $code   if $data is an error object, return true
  236.      *                        only if $code is a string and
  237.      *                        $obj->getMessage() == $code or
  238.      *                        $code is an integer and $obj->getCode() == $code
  239.      * @access  public
  240.      * @return  bool    true if parameter is an error
  241.      */
  242.     function isError($data, $code = null)
  243.     {
  244.         if (is_a($data, 'PEAR_Error')) {
  245.             if (is_null($code)) {
  246.                 return true;
  247.             } elseif (is_string($code)) {
  248.                 return $data->getMessage() == $code;
  249.             } else {
  250.                 return $data->getCode() == $code;
  251.             }
  252.         }
  253.         return false;
  254.     }
  255.  
  256.     // }}}
  257.     // {{{ setErrorHandling()
  258.  
  259.     /**
  260.      * Sets how errors generated by this object should be handled.
  261.      * Can be invoked both in objects and statically.  If called
  262.      * statically, setErrorHandling sets the default behaviour for all
  263.      * PEAR objects.  If called in an object, setErrorHandling sets
  264.      * the default behaviour for that object.
  265.      *
  266.      * @param int $mode
  267.      *        One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
  268.      *        PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
  269.      *        PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION.
  270.      *
  271.      * @param mixed $options
  272.      *        When $mode is PEAR_ERROR_TRIGGER, this is the error level (one
  273.      *        of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
  274.      *
  275.      *        When $mode is PEAR_ERROR_CALLBACK, this parameter is expected
  276.      *        to be the callback function or method.  A callback
  277.      *        function is a string with the name of the function, a
  278.      *        callback method is an array of two elements: the element
  279.      *        at index 0 is the object, and the element at index 1 is
  280.      *        the name of the method to call in the object.
  281.      *
  282.      *        When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is
  283.      *        a printf format string used when printing the error
  284.      *        message.
  285.      *
  286.      * @access public
  287.      * @return void
  288.      * @see PEAR_ERROR_RETURN
  289.      * @see PEAR_ERROR_PRINT
  290.      * @see PEAR_ERROR_TRIGGER
  291.      * @see PEAR_ERROR_DIE
  292.      * @see PEAR_ERROR_CALLBACK
  293.      * @see PEAR_ERROR_EXCEPTION
  294.      *
  295.      * @since PHP 4.0.5
  296.      */
  297.  
  298.     function setErrorHandling($mode = null, $options = null)
  299.     {
  300.         if (isset($this) && is_a($this, 'PEAR')) {
  301.             $setmode     = &$this->_default_error_mode;
  302.             $setoptions  = &$this->_default_error_options;
  303.         } else {
  304.             $setmode     = &$GLOBALS['_PEAR_default_error_mode'];
  305.             $setoptions  = &$GLOBALS['_PEAR_default_error_options'];
  306.         }
  307.  
  308.         switch ($mode) {
  309.             case PEAR_ERROR_RETURN:
  310.             case PEAR_ERROR_PRINT:
  311.             case PEAR_ERROR_TRIGGER:
  312.             case PEAR_ERROR_DIE:
  313.             case PEAR_ERROR_EXCEPTION:
  314.             case null:
  315.                 $setmode = $mode;
  316.                 $setoptions = $options;
  317.                 break;
  318.  
  319.             case PEAR_ERROR_CALLBACK:
  320.                 $setmode = $mode;
  321.                 // class/object method callback
  322.                 if (is_callable($options)) {
  323.                     $setoptions = $options;
  324.                 } else {
  325.                     trigger_error("invalid error callback", E_USER_WARNING);
  326.                 }
  327.                 break;
  328.  
  329.             default:
  330.                 trigger_error("invalid error mode", E_USER_WARNING);
  331.                 break;
  332.         }
  333.     }
  334.  
  335.     // }}}
  336.     // {{{ expectError()
  337.  
  338.     /**
  339.      * This method is used to tell which errors you expect to get.
  340.      * Expected errors are always returned with error mode
  341.      * PEAR_ERROR_RETURN.  Expected error codes are stored in a stack,
  342.      * and this method pushes a new element onto it.  The list of
  343.      * expected errors are in effect until they are popped off the
  344.      * stack with the popExpect() method.
  345.      *
  346.      * Note that this method can not be called statically
  347.      *
  348.      * @param mixed $code a single error code or an array of error codes to expect
  349.      *
  350.      * @return int     the new depth of the "expected errors" stack
  351.      * @access public
  352.      */
  353.     function expectError($code = '*')
  354.     {
  355.         if (is_array($code)) {
  356.             array_push($this->_expected_errors, $code);
  357.         } else {
  358.             array_push($this->_expected_errors, array($code));
  359.         }
  360.         return sizeof($this->_expected_errors);
  361.     }
  362.  
  363.     // }}}
  364.     // {{{ popExpect()
  365.  
  366.     /**
  367.      * This method pops one element off the expected error codes
  368.      * stack.
  369.      *
  370.      * @return array   the list of error codes that were popped
  371.      */
  372.     function popExpect()
  373.     {
  374.         return array_pop($this->_expected_errors);
  375.     }
  376.  
  377.     // }}}
  378.     // {{{ _checkDelExpect()
  379.  
  380.     /**
  381.      * This method checks unsets an error code if available
  382.      *
  383.      * @param mixed error code
  384.      * @return bool true if the error code was unset, false otherwise
  385.      * @access private
  386.      * @since PHP 4.3.0
  387.      */
  388.     function _checkDelExpect($error_code)
  389.     {
  390.         $deleted = false;
  391.  
  392.         foreach ($this->_expected_errors AS $key => $error_array) {
  393.             if (in_array($error_code, $error_array)) {
  394.                 unset($this->_expected_errors[$key][array_search($error_code, $error_array)]);
  395.                 $deleted = true;
  396.             }
  397.  
  398.             // clean up empty arrays
  399.             if (0 == count($this->_expected_errors[$key])) {
  400.                 unset($this->_expected_errors[$key]);
  401.             }
  402.         }
  403.         return $deleted;
  404.     }
  405.  
  406.     // }}}
  407.     // {{{ delExpect()
  408.  
  409.     /**
  410.      * This method deletes all occurences of the specified element from
  411.      * the expected error codes stack.
  412.      *
  413.      * @param  mixed $error_code error code that should be deleted
  414.      * @return mixed list of error codes that were deleted or error
  415.      * @access public
  416.      * @since PHP 4.3.0
  417.      */
  418.     function delExpect($error_code)
  419.     {
  420.         $deleted = false;
  421.  
  422.         if ((is_array($error_code) && (0 != count($error_code)))) {
  423.             // $error_code is a non-empty array here;
  424.             // we walk through it trying to unset all
  425.             // values
  426.             foreach($error_code as $key => $error) {
  427.                 if ($this->_checkDelExpect($error)) {
  428.                     $deleted =  true;
  429.                 } else {
  430.                     $deleted = false;
  431.                 }
  432.             }
  433.             return $deleted ? true : PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
  434.         } elseif (!empty($error_code)) {
  435.             // $error_code comes alone, trying to unset it
  436.             if ($this->_checkDelExpect($error_code)) {
  437.                 return true;
  438.             } else {
  439.                 return PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
  440.             }
  441.         } else {
  442.             // $error_code is empty
  443.             return PEAR::raiseError("The expected error you submitted is empty"); // IMPROVE ME
  444.         }
  445.     }
  446.  
  447.     // }}}
  448.     // {{{ raiseError()
  449.  
  450.     /**
  451.      * This method is a wrapper that returns an instance of the
  452.      * configured error class with this object's default error
  453.      * handling applied.  If the $mode and $options parameters are not
  454.      * specified, the object's defaults are used.
  455.      *
  456.      * @param mixed $message a text error message or a PEAR error object
  457.      *
  458.      * @param int $code      a numeric error code (it is up to your class
  459.      *                  to define these if you want to use codes)
  460.      *
  461.      * @param int $mode      One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
  462.      *                  PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
  463.      *                  PEAR_ERROR_CALLBACK, PEAR_ERROR_EXCEPTION.
  464.      *
  465.      * @param mixed $options If $mode is PEAR_ERROR_TRIGGER, this parameter
  466.      *                  specifies the PHP-internal error level (one of
  467.      *                  E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
  468.      *                  If $mode is PEAR_ERROR_CALLBACK, this
  469.      *                  parameter specifies the callback function or
  470.      *                  method.  In other error modes this parameter
  471.      *                  is ignored.
  472.      *
  473.      * @param string $userinfo If you need to pass along for example debug
  474.      *                  information, this parameter is meant for that.
  475.      *
  476.      * @param string $error_class The returned error object will be
  477.      *                  instantiated from this class, if specified.
  478.      *
  479.      * @param bool $skipmsg If true, raiseError will only pass error codes,
  480.      *                  the error message parameter will be dropped.
  481.      *
  482.      * @access public
  483.      * @return object   a PEAR error object
  484.      * @see PEAR::setErrorHandling
  485.      * @since PHP 4.0.5
  486.      */
  487.     function raiseError($message = null,
  488.                          $code = null,
  489.                          $mode = null,
  490.                          $options = null,
  491.                          $userinfo = null,
  492.                          $error_class = null,
  493.                          $skipmsg = false)
  494.     {
  495.         // The error is yet a PEAR error object
  496.         if (is_object($message)) {
  497.             $code        = $message->getCode();
  498.             $userinfo    = $message->getUserInfo();
  499.             $error_class = $message->getType();
  500.             $message->error_message_prefix = '';
  501.             $message     = $message->getMessage();
  502.         }
  503.  
  504.         if (isset($this) && isset($this->_expected_errors) && sizeof($this->_expected_errors) > 0 && sizeof($exp = end($this->_expected_errors))) {
  505.             if ($exp[0] == "*" ||
  506.                 (is_int(reset($exp)) && in_array($code, $exp)) ||
  507.                 (is_string(reset($exp)) && in_array($message, $exp))) {
  508.                 $mode = PEAR_ERROR_RETURN;
  509.             }
  510.         }
  511.         // No mode given, try global ones
  512.         if ($mode === null) {
  513.             // Class error handler
  514.             if (isset($this) && isset($this->_default_error_mode)) {
  515.                 $mode    = $this->_default_error_mode;
  516.                 $options = $this->_default_error_options;
  517.             // Global error handler
  518.             } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) {
  519.                 $mode    = $GLOBALS['_PEAR_default_error_mode'];
  520.                 $options = $GLOBALS['_PEAR_default_error_options'];
  521.             }
  522.         }
  523.  
  524.         if ($error_class !== null) {
  525.             $ec = $error_class;
  526.         } elseif (isset($this) && isset($this->_error_class)) {
  527.             $ec = $this->_error_class;
  528.         } else {
  529.             $ec = 'PEAR_Error';
  530.         }
  531.         if ($skipmsg) {
  532.             return new $ec($code, $mode, $options, $userinfo);
  533.         } else {
  534.             return new $ec($message, $code, $mode, $options, $userinfo);
  535.         }
  536.     }
  537.  
  538.     // }}}
  539.     // {{{ throwError()
  540.  
  541.     /**
  542.      * Simpler form of raiseError with fewer options.  In most cases
  543.      * message, code and userinfo are enough.
  544.      *
  545.      * @param string $message
  546.      *
  547.      */
  548.     function throwError($message = null,
  549.                          $code = null,
  550.                          $userinfo = null)
  551.     {
  552.         if (isset($this) && is_subclass_of($this, 'PEAR_Error')) {
  553.             return $this->raiseError($message, $code, null, null, $userinfo);
  554.         } else {
  555.             return PEAR::raiseError($message, $code, null, null, $userinfo);
  556.         }
  557.     }
  558.  
  559.     // }}}
  560.     // {{{ pushErrorHandling()
  561.  
  562.     /**
  563.      * Push a new error handler on top of the error handler options stack. With this
  564.      * you can easily override the actual error handler for some code and restore
  565.      * it later with popErrorHandling.
  566.      *
  567.      * @param mixed $mode (same as setErrorHandling)
  568.      * @param mixed $options (same as setErrorHandling)
  569.      *
  570.      * @return bool Always true
  571.      *
  572.      * @see PEAR::setErrorHandling
  573.      */
  574.     function pushErrorHandling($mode, $options = null)
  575.     {
  576.         $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  577.         if (isset($this) && is_a($this, 'PEAR')) {
  578.             $def_mode    = &$this->_default_error_mode;
  579.             $def_options = &$this->_default_error_options;
  580.         } else {
  581.             $def_mode    = &$GLOBALS['_PEAR_default_error_mode'];
  582.             $def_options = &$GLOBALS['_PEAR_default_error_options'];
  583.         }
  584.         $stack[] = array($def_mode, $def_options);
  585.  
  586.         if (isset($this) && is_a($this, 'PEAR')) {
  587.             $this->setErrorHandling($mode, $options);
  588.         } else {
  589.             PEAR::setErrorHandling($mode, $options);
  590.         }
  591.         $stack[] = array($mode, $options);
  592.         return true;
  593.     }
  594.  
  595.     // }}}
  596.     // {{{ popErrorHandling()
  597.  
  598.     /**
  599.     * Pop the last error handler used
  600.     *
  601.     * @return bool Always true
  602.     *
  603.     * @see PEAR::pushErrorHandling
  604.     */
  605.     function popErrorHandling()
  606.     {
  607.         $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  608.         array_pop($stack);
  609.         list($mode, $options) = $stack[sizeof($stack) - 1];
  610.         array_pop($stack);
  611.         if (isset($this) && is_a($this, 'PEAR')) {
  612.             $this->setErrorHandling($mode, $options);
  613.         } else {
  614.             PEAR::setErrorHandling($mode, $options);
  615.         }
  616.         return true;
  617.     }
  618.  
  619.     // }}}
  620.     // {{{ loadExtension()
  621.  
  622.     /**
  623.     * OS independant PHP extension load. Remember to take care
  624.     * on the correct extension name for case sensitive OSes.
  625.     *
  626.     * @param string $ext The extension name
  627.     * @return bool Success or not on the dl() call
  628.     */
  629.     function loadExtension($ext)
  630.     {
  631.         if (!extension_loaded($ext)) {
  632.             // if either returns true dl() will produce a FATAL error, stop that
  633.             if ((ini_get('enable_dl') != 1) || (ini_get('safe_mode') == 1)) {
  634.                 return false;
  635.             }
  636.             if (OS_WINDOWS) {
  637.                 $suffix = '.dll';
  638.             } elseif (PHP_OS == 'HP-UX') {
  639.                 $suffix = '.sl';
  640.             } elseif (PHP_OS == 'AIX') {
  641.                 $suffix = '.a';
  642.             } elseif (PHP_OS == 'OSX') {
  643.                 $suffix = '.bundle';
  644.             } else {
  645.                 $suffix = '.so';
  646.             }
  647.             return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
  648.         }
  649.         return true;
  650.     }
  651.  
  652.     // }}}
  653. }
  654.  
  655. // {{{ _PEAR_call_destructors()
  656.  
  657. function _PEAR_call_destructors()
  658. {
  659.     global $_PEAR_destructor_object_list;
  660.     if (is_array($_PEAR_destructor_object_list) &&
  661.         sizeof($_PEAR_destructor_object_list))
  662.     {
  663.         reset($_PEAR_destructor_object_list);
  664.         while (list($k, $objref) = each($_PEAR_destructor_object_list)) {
  665.             $classname = get_class($objref);
  666.             while ($classname) {
  667.                 $destructor = "_$classname";
  668.                 if (method_exists($objref, $destructor)) {
  669.                     $objref->$destructor();
  670.                     break;
  671.                 } else {
  672.                     $classname = get_parent_class($classname);
  673.                 }
  674.             }
  675.         }
  676.         // Empty the object list to ensure that destructors are
  677.         // not called more than once.
  678.         $_PEAR_destructor_object_list = array();
  679.     }
  680.  
  681.     // Now call the shutdown functions
  682.     if (is_array($GLOBALS['_PEAR_shutdown_funcs']) AND !empty($GLOBALS['_PEAR_shutdown_funcs'])) {
  683.         foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) {
  684.             call_user_func_array($value[0], $value[1]);
  685.         }
  686.     }
  687. }
  688.  
  689. // }}}
  690.  
  691. class PEAR_Error
  692. {
  693.     // {{{ properties
  694.  
  695.     var $error_message_prefix = '';
  696.     var $mode                 = PEAR_ERROR_RETURN;
  697.     var $level                = E_USER_NOTICE;
  698.     var $code                 = -1;
  699.     var $message              = '';
  700.     var $userinfo             = '';
  701.     var $backtrace            = null;
  702.  
  703.     // }}}
  704.     // {{{ constructor
  705.  
  706.     /**
  707.      * PEAR_Error constructor
  708.      *
  709.      * @param string $message  message
  710.      *
  711.      * @param int $code     (optional) error code
  712.      *
  713.      * @param int $mode     (optional) error mode, one of: PEAR_ERROR_RETURN,
  714.      * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER,
  715.      * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION
  716.      *
  717.      * @param mixed $options   (optional) error level, _OR_ in the case of
  718.      * PEAR_ERROR_CALLBACK, the callback function or object/method
  719.      * tuple.
  720.      *
  721.      * @param string $userinfo (optional) additional user/debug info
  722.      *
  723.      * @access public
  724.      *
  725.      */
  726.     function PEAR_Error($message = 'unknown error', $code = null,
  727.                         $mode = null, $options = null, $userinfo = null)
  728.     {
  729.         if ($mode === null) {
  730.             $mode = PEAR_ERROR_RETURN;
  731.         }
  732.         $this->message   = $message;
  733.         $this->code      = $code;
  734.         $this->mode      = $mode;
  735.         $this->userinfo  = $userinfo;
  736.         if (function_exists("debug_backtrace")) {
  737.             $this->backtrace = debug_backtrace();
  738.         }
  739.         if ($mode & PEAR_ERROR_CALLBACK) {
  740.             $this->level = E_USER_NOTICE;
  741.             $this->callback = $options;
  742.         } else {
  743.             if ($options === null) {
  744.                 $options = E_USER_NOTICE;
  745.             }
  746.             $this->level = $options;
  747.             $this->callback = null;
  748.         }
  749.         if ($this->mode & PEAR_ERROR_PRINT) {
  750.             if (is_null($options) || is_int($options)) {
  751.                 $format = "%s";
  752.             } else {
  753.                 $format = $options;
  754.             }
  755.             printf($format, $this->getMessage());
  756.         }
  757.         if ($this->mode & PEAR_ERROR_TRIGGER) {
  758.             trigger_error($this->getMessage(), $this->level);
  759.         }
  760.         if ($this->mode & PEAR_ERROR_DIE) {
  761.             $msg = $this->getMessage();
  762.             if (is_null($options) || is_int($options)) {
  763.                 $format = "%s";
  764.                 if (substr($msg, -1) != "\n") {
  765.                     $msg .= "\n";
  766.                 }
  767.             } else {
  768.                 $format = $options;
  769.             }
  770.             die(sprintf($format, $msg));
  771.         }
  772.         if ($this->mode & PEAR_ERROR_CALLBACK) {
  773.             if (is_callable($this->callback)) {
  774.                 call_user_func($this->callback, $this);
  775.             }
  776.         }
  777.         if (PEAR_ZE2 && $this->mode & PEAR_ERROR_EXCEPTION) {
  778.             eval('throw $this;');
  779.         }
  780.     }
  781.  
  782.     // }}}
  783.     // {{{ getMode()
  784.  
  785.     /**
  786.      * Get the error mode from an error object.
  787.      *
  788.      * @return int error mode
  789.      * @access public
  790.      */
  791.     function getMode() {
  792.         return $this->mode;
  793.     }
  794.  
  795.     // }}}
  796.     // {{{ getCallback()
  797.  
  798.     /**
  799.      * Get the callback function/method from an error object.
  800.      *
  801.      * @return mixed callback function or object/method array
  802.      * @access public
  803.      */
  804.     function getCallback() {
  805.         return $this->callback;
  806.     }
  807.  
  808.     // }}}
  809.     // {{{ getMessage()
  810.  
  811.  
  812.     /**
  813.      * Get the error message from an error object.
  814.      *
  815.      * @return  string  full error message
  816.      * @access public
  817.      */
  818.     function getMessage()
  819.     {
  820.         return ($this->error_message_prefix . $this->message);
  821.     }
  822.  
  823.  
  824.     // }}}
  825.     // {{{ getCode()
  826.  
  827.     /**
  828.      * Get error code from an error object
  829.      *
  830.      * @return int error code
  831.      * @access public
  832.      */
  833.      function getCode()
  834.      {
  835.         return $this->code;
  836.      }
  837.  
  838.     // }}}
  839.     // {{{ getType()
  840.  
  841.     /**
  842.      * Get the name of this error/exception.
  843.      *
  844.      * @return string error/exception name (type)
  845.      * @access public
  846.      */
  847.     function getType()
  848.     {
  849.         return get_class($this);
  850.     }
  851.  
  852.     // }}}
  853.     // {{{ getUserInfo()
  854.  
  855.     /**
  856.      * Get additional user-supplied information.
  857.      *
  858.      * @return string user-supplied information
  859.      * @access public
  860.      */
  861.     function getUserInfo()
  862.     {
  863.         return $this->userinfo;
  864.     }
  865.  
  866.     // }}}
  867.     // {{{ getDebugInfo()
  868.  
  869.     /**
  870.      * Get additional debug information supplied by the application.
  871.      *
  872.      * @return string debug information
  873.      * @access public
  874.      */
  875.     function getDebugInfo()
  876.     {
  877.         return $this->getUserInfo();
  878.     }
  879.  
  880.     // }}}
  881.     // {{{ getBacktrace()
  882.  
  883.     /**
  884.      * Get the call backtrace from where the error was generated.
  885.      * Supported with PHP 4.3.0 or newer.
  886.      *
  887.      * @param int $frame (optional) what frame to fetch
  888.      * @return array Backtrace, or NULL if not available.
  889.      * @access public
  890.      */
  891.     function getBacktrace($frame = null)
  892.     {
  893.         if ($frame === null) {
  894.             return $this->backtrace;
  895.         }
  896.         return $this->backtrace[$frame];
  897.     }
  898.  
  899.     // }}}
  900.     // {{{ addUserInfo()
  901.  
  902.     function addUserInfo($info)
  903.     {
  904.         if (empty($this->userinfo)) {
  905.             $this->userinfo = $info;
  906.         } else {
  907.             $this->userinfo .= " ** $info";
  908.         }
  909.     }
  910.  
  911.     // }}}
  912.     // {{{ toString()
  913.  
  914.     /**
  915.      * Make a string representation of this object.
  916.      *
  917.      * @return string a string with an object summary
  918.      * @access public
  919.      */
  920.     function toString() {
  921.         $modes = array();
  922.         $levels = array(E_USER_NOTICE  => 'notice',
  923.                         E_USER_WARNING => 'warning',
  924.                         E_USER_ERROR   => 'error');
  925.         if ($this->mode & PEAR_ERROR_CALLBACK) {
  926.             if (is_array($this->callback)) {
  927.                 $callback = get_class($this->callback[0]) . '::' .
  928.                     $this->callback[1];
  929.             } else {
  930.                 $callback = $this->callback;
  931.             }
  932.             return sprintf('[%s: message="%s" code=%d mode=callback '.
  933.                            'callback=%s prefix="%s" info="%s"]',
  934.                            get_class($this), $this->message, $this->code,
  935.                            $callback, $this->error_message_prefix,
  936.                            $this->userinfo);
  937.         }
  938.         if ($this->mode & PEAR_ERROR_PRINT) {
  939.             $modes[] = 'print';
  940.         }
  941.         if ($this->mode & PEAR_ERROR_TRIGGER) {
  942.             $modes[] = 'trigger';
  943.         }
  944.         if ($this->mode & PEAR_ERROR_DIE) {
  945.             $modes[] = 'die';
  946.         }
  947.         if ($this->mode & PEAR_ERROR_RETURN) {
  948.             $modes[] = 'return';
  949.         }
  950.         return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
  951.                        'prefix="%s" info="%s"]',
  952.                        get_class($this), $this->message, $this->code,
  953.                        implode("|", $modes), $levels[$this->level],
  954.                        $this->error_message_prefix,
  955.                        $this->userinfo);
  956.     }
  957.  
  958.     // }}}
  959. }
  960.  
  961. register_shutdown_function("_PEAR_call_destructors");
  962.  
  963. /*
  964.  * Local Variables:
  965.  * mode: php
  966.  * tab-width: 4
  967.  * c-basic-offset: 4
  968.  * End:
  969.  */
  970. ?>
  971.